package com.ruoyi.web.wx.controller;

import java.util.*;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.web.wx.entity.Address;
import com.ruoyi.web.wx.entity.CommunityManager;
import com.ruoyi.web.wx.service.IAddressService;
import com.ruoyi.web.wx.service.TokenService;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.web.wx.service.ICommunityManagerService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 社区员 信息操作处理
 * 
 * @author ruoyi
 * @date 2019-08-27
 */
@Controller
@RequestMapping("/system/communityManager")
public class CommunityManagerController extends BaseController
{
    private String prefix = "system/communityManager";
	
	@Autowired
	private ICommunityManagerService communityManagerService;
	@Autowired
	private IAddressService addressService;
	@Autowired
	private TokenService tokenService;
	
	@RequiresPermissions("system:communityManager:view")
	@GetMapping()
	public String communityManager()
	{
	    return prefix + "/communityManager";
	}
	
	/**
	 * 查询社区员列表
	 */
	@RequiresPermissions("system:communityManager:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CommunityManager communityManager)
	{
		startPage();
        List<CommunityManager> list = communityManagerService.selectCommunityManagerList(communityManager);
		return getDataTable(list);
	}
	
	
	/**
	 * 导出社区员列表
	 */
	@RequiresPermissions("system:communityManager:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CommunityManager communityManager)
    {
    	List<CommunityManager> list = communityManagerService.selectCommunityManagerList(communityManager);
        ExcelUtil<CommunityManager> util = new ExcelUtil<CommunityManager>(CommunityManager.class);
        return util.exportExcel(list, "社区管理员");
    }
	
	/**
	 * 新增社区员
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存社区员
	 */
	@RequiresPermissions("system:communityManager:add")
	@Log(title = "社区员", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(CommunityManager communityManager)
	{
		List<String> village = communityManager.getVillage();
		communityManager.setCreateTime(new Date());
		String join = String.join(",", village);
		communityManager.setVillages(join);
		String password = communityManager.getPassword();
		String s = DigestUtils.sha256Hex(password);
		communityManager.setPassword(s);
		communityManager.setId(UUID.randomUUID().toString().replaceAll("-",""));
		return toAjax(communityManagerService.insertCommunityManager(communityManager));
	}

	/**
	 * 修改社区员
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap mmap)
	{
		CommunityManager communityManager = communityManagerService.selectCommunityManagerById(id);
		List<String> result = Arrays.asList(communityManager.getVillages().split(","));
		communityManager.setVillage(result);
		List<Address> addresses = addressService.selectAddressList(new Address());
		communityManager.setVillagess(addresses);
		mmap.put("communityManager", communityManager);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存社区员
	 */
	@RequiresPermissions("system:communityManager:edit")
	@Log(title = "社区员", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(CommunityManager communityManager)
	{
		String status = communityManager.getStatus();
		if ("0".equals(status)) {
		//清除登录状态
			String[] split = communityManager.getId().split(",");
			tokenService.delete(split);
		}
		List<String> village = communityManager.getVillage();
		String join = String.join(",", village);
		communityManager.setVillages(join);
		return toAjax(communityManagerService.updateCommunityManager(communityManager));
	}
	
	/**
	 * 删除社区员
	 */
	@RequiresPermissions("system:communityManager:remove")
	@Log(title = "社区员", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{
		//清除登录状态
		if (StringUtils.isNotEmpty(ids)){
			String[] split = ids.split(",");
			tokenService.delete(split);
		}
		return toAjax(communityManagerService.deleteCommunityManagerByIds(ids));
	}
	
}
